package cn.edu.nju.askinginhand.activity;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainTab extends ActivityGroup {

	/** The 5 module buttons in the bottom bar */
	private ImageView btnModule1;//个人界面
	private ImageView btnModule2;//音频问题
	private ImageView btnModule3;//图片问题
	private ImageView btnModule4;//好友列表
	private ImageView btnModule5;//新鲜事
	private FrameLayout container;//
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_tab);
		btnModule1 = (ImageView) findViewById(R.id.btnModule1);
		btnModule2 = (ImageView) findViewById(R.id.btnModule2);
		btnModule3 = (ImageView) findViewById(R.id.btnModule3);
		btnModule4 = (ImageView) findViewById(R.id.btnModule4);
		btnModule5 = (ImageView) findViewById(R.id.btnModule5);
		container = (FrameLayout) findViewById(R.id.container);

		setMenuListeners();
	}

	private void setMenuListeners() {
		// 个人页面
		btnModule1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity(
						"HomeActivity",
						new Intent(MainTab.this, HomeActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
				// btnModule1.setFadingEdgeLength(10);
			}
		});
		btnModule1.performClick();

		//声音问题
		btnModule2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity(
						"AudioQuesListActivity",
						new Intent(MainTab.this, AudioQuesListActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
			}
		});

		//照片问题
		btnModule3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity(
						"PictureQuesListActivity",
						new Intent(MainTab.this, PictureQuesListActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
			}
		});

		// 好友
		btnModule4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity(
						"FriendListActivity",
						new Intent(MainTab.this, FriendListActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
			}
		});

		//新鲜事
		btnModule5.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				container.removeAllViews();
				container.addView(getLocalActivityManager().startActivity(
						"FreshListActivity",
						new Intent(MainTab.this, FreshListActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView());
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// TODO : 弹出窗口询问是否退出& 返回true
			this.onDestroy();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	/**
	 * 此activity结束时退出程序
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.exit(0);
		//android.os.Process.killProcess(android.os.Process.myPid());
	}
}
